package com.hdax.dm.service;

import com.hdax.dm.dto.CommonResponse;
import com.hdax.dm.dto.DtoUtil;
import com.hdax.dm.dto.item.ItemCommentDto;
import com.hdax.dm.entity.base.DmImage;
import com.hdax.dm.entity.item.DmItemComment;
import com.hdax.dm.feign.base.DmImageFeign;
import com.hdax.dm.feign.item.DmItemCommentFeign;
import com.hdax.dm.utils.MokeImage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DmItemCommentService {

    @Resource
    private DmItemCommentFeign dmItemCommentFeign;
    @Resource
    private DmImageFeign dmImageFeign;

    //根据itemid获取到评论结合
    public CommonResponse<ItemCommentDto> getComments(Long id){
        //属性拷贝
        List<DmItemComment> itemComments = dmItemCommentFeign.dmItemComment(id);
        List<ItemCommentDto> itemCommentDtos = new ArrayList<>();
        itemComments.forEach(itemComment->{
            ItemCommentDto itemCommentDto = new ItemCommentDto();
            BeanUtils.copyProperties(itemComment,itemCommentDto);
            DmImage dmImage = dmImageFeign.getImage(0L,0L,itemComment.getUserId());
            itemCommentDto.setImgUrl(dmImage==null? MokeImage.getImage(1):dmImage.getImgUrl());
            itemCommentDtos.add(itemCommentDto);
        });
        return DtoUtil.returnSuccess(itemCommentDtos);
    }

    //添加剧评
    public CommonResponse<String> addComments(DmItemComment dmItemComment){
        return DtoUtil.returnSuccess(dmItemCommentFeign.addComments(dmItemComment)) ;
    }
}
